using System;
using System.Data.SQLite;

namespace SQLiteHelper
{
    public class SqLite : IDisposable
    {
        public string ConnectionString { get; set; }

        private SQLiteConnection _conn;
        private SQLiteConnection Conn
        {
            get
            {
                if (_conn == null)
                {
                    if (string.IsNullOrEmpty(ConnectionString))
                        throw new ArgumentException("Connection string must be setted brfore open a connection.");
                    _conn = new SQLiteConnection(ConnectionString);
                    _conn.Open();
                }
                return _conn;
            }
        }

        public SqLite(string connectionString)
        {
            ConnectionString = connectionString;
        }

        public void ExcuteNonquery(string command)
        {
            SQLiteCommand sqLiteCommand = Conn.CreateCommand();
            sqLiteCommand.CommandText = command;
            sqLiteCommand.ExecuteNonQuery();
        }

        public SQLiteDataReader ExcuteReader(string command)
        {
            SQLiteCommand sqLiteCommand = Conn.CreateCommand();
            sqLiteCommand.CommandText = command;
            return sqLiteCommand.ExecuteReader();
        }

        public object ExceuteScalar(string command)
        {
            SQLiteCommand sqLiteCommand = Conn.CreateCommand();
            sqLiteCommand.CommandText = command;
            return sqLiteCommand.ExecuteScalar();
        }

        public void Dispose()
        {
            if (_conn != null)
                _conn.Clone();
        }
    }
}